#include "../include/sink.hpp"
#include <cassert>
#include <thread>
#include <utility>

using namespace easylog;

void test_stdout_sink(message&& msg)
{
    sinker::ptr ss = sinker_factory::create<stdout_sinker>();
    ss->set_color(true);
    // ss->set_logfile("");
    ss->sink(std::forward<message>(msg));
}

void test_file_sink(message&& msg)
{
    sinker::ptr fs = sinker_factory::create<file_sinker>();
    fs->set_fn_patt("log-%d{%F-%H%M%S}.log");
    fs->sink(std::forward<message>(msg));
}

void test_roll_over_time(message&& msg)
{
    sinker::ptr rot = sinker_factory::create<roll_over_time>(time_enum::t2sec);
    rot->set_max_history(5);
    rot->set_fn_patt("logs/log-%d{%F-%H%M%S}-%i.log");

    while (1)
    {
        rot->sink(std::forward<message>(msg));
        // std::this_thread::sleep_for(std::chrono::microseconds(10));
    }
}

void test_roll_over_size(message&& msg)
{
    sinker::ptr ros = sinker_factory::create<roll_over_size>(size_enum::m5MB);
    ros->set_max_history(10);
    ros->set_fn_patt("logs/log-%d{%F-%H%M%S}-%i.log");

    while (1)
    {
        ros->sink(std::forward<message>(msg));
        // std::this_thread::sleep_for(std::chrono::microseconds(10));
    }
}

void test_flush(message&& msg)
{
    sinker::ptr fs = sinker_factory::create<file_sinker>();
    fs->set_logfile("test_log.txt");

    fs->sink(std::forward<message>(msg));

    std::this_thread::sleep_for(std::chrono::seconds(3));
    std::cout << "flush now" << std::endl;
    fs->flush();
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "quit now" << std::endl;
}

#define TEST \
                5

int main()
{
    message msg(level::lv_enum::info, __FILE__, __LINE__, __FUNCTION__, "logger1", "I get a message");

    switch (TEST)
    {
    case 1: test_stdout_sink(std::forward<message>(msg));
            break;
    case 2: test_file_sink(std::forward<message>(msg));
            break;
    case 3: test_roll_over_time(std::forward<message>(msg));
            break;
    case 4: test_roll_over_size(std::forward<message>(msg));
            break;
    case 5: test_flush(std::forward<message>(msg));
            break;
    default: assert(false);
    }

    return 0;
}
